// runs on website startup
function init(){
	
	var content = d3.select("#content");
	
	// top svg element
	var svg = content.append("svg")
		.attr("width", "100%")
		.attr("height", "100%")
		.attr("id", "svgTop");
	
	// svg for tree map
	var svgTree = svg.append("svg")
		.attr("id", "svgTree");
	addBoundingRect(svgTree);

	// svg for details on demand
	var svgDod = svg.append("svg")
		.attr("x", "80%")
		.attr("id", "svgDod");	
	addBoundingRect(svgDod);
	
	// svg for pie chart
	var pieSize = 20;
	var svgPie = svg.append("svg")
		.attr("y", (100-pieSize)+"%")
		.attr("height", (pieSize)+"%")
		.attr("id", "svgPie");
	addBoundingRect(svgPie);
	svgPie.attr("width", document.getElementById(svgPie.attr("id")).getBBox().height);
	
	// reads each line in csv
	d3.tsv("FileFinder/results.tsv", function(data){
		
		// puts each file/folder as an array object in files
		var temp = data.map(function(d){
			return{
				name:d.name,
				size:d.size,
				isFolder:d.isfolder==="Y",
				parent:+d.parent,
				modTime:+d.moddate,
				modTimeString: new Date().toUTCString(+d.moddate)
			}
		});
		
		files = [];
		// iterate over files/folders in array
		for (var i=0; i<temp.length; i++){
			// folder
			if (temp[i].isFolder) files.push(new Folder(temp[i], files));
			// file
			else files.push(new File(temp[i], files));
		}
		
		// root is always the root of the folder tree
		root = files[0];
		
		// curr changes based on user interaction
		curr = root;
		
		// console.log the path name of each file and folder
		var folders = [root];// folder queue
		while (folders.length>0){
			var currentFolder = folders.shift();
			
			// do stuff to current folder
			console.log(currentFolder.path);
			
			// do something to each file child in the current folder
			for (var i=0; i<currentFolder.fileChildren.length; i++){
				// do stuff to file
				console.log(currentFolder.fileChildren[i].path+", "+currentFolder.fileChildren[i].type);
			}
			
			// push each folder to the folder queue
			for (var i=0; i<currentFolder.folderChildren.length; i++){
				folders.push(currentFolder.folderChildren[i]);
			}
			
		}
		
		
		// draws all visualizations
		drawTreeMap(svgTree);
		drawDetailsOnDemand(svgDod);
		drawPieChart(svgPie);
		
	});
	
}

// add faint grey rectangle
function addBoundingRect(svgElement){
	svgElement.append("rect")
		.attr("width","100%")
		.attr("height","100%")
		.attr("fill", "none")
		.attr("class", "boundingRect")
		.attr("stroke-width", "3px")
		.attr("stroke", "#aaaaaa");
}

// Contains all data for a file
function File(obj, files){
	this.name = obj.name;
	this.size = obj.size;
	this.modTime = obj.modTime;
	this.parent = files[obj.parent];	
	this.parent.fileChildren.push(this);
	this.path = getPathName(this);
	var dotSplit = obj.name.split(".");
	if (dotSplit.length>1) this.type = dotSplit[dotSplit.length-1].toLocaleLowerCase();
	else this.type = "";
}

// Contains all data for a folder
function Folder(obj, files){
	this.name = obj.name;
	this.size = obj.size;	
	this.modTime = obj.modTime;
	if (obj.parent>=0){
		this.parent = files[obj.parent];
		this.parent.folderChildren.push(this);
	}
	else this.parent = null;
	this.fileChildren = [];
	this.folderChildren = [];
	this.path = getPathName(this);
}

// Used by File and Folder constructors to construct the path name
function getPathName(obj){
	var path = obj.name;
	curr = obj.parent;
	while (curr!=null){
		path = curr.name+"/"+path;
		curr = curr.parent;
	}
	return path;
}